package com.blyat.xsoft.kernel.restful.controller;

import com.blyat.xsoft.kernel.restful.entity.BaseEntity;
import com.blyat.xsoft.kernel.restful.service.BaseService;
import com.blyat.xsoft.kernel.restful.view.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * @Auther: syh
 * @Date: 2020/8/26
 * @Description:
 */
public class BaseController<O extends BaseEntity, T extends BaseService<O>> {

    @Autowired
    protected T service;

    @ResponseBody
    @GetMapping("{id}")
    public ResultModel get(@PathVariable("id") String id) {
        return ResultModel.SUCCESS(service.getById(id));
    }

    @ResponseBody
    @PostMapping
    public ResultModel add(@NotNull @RequestBody O entity) {
        return ResultModel.SUCCESS(service.insert(entity));
    }

    @ResponseBody
    @PutMapping
    public ResultModel update(@NotNull @RequestBody O entity) {
        return ResultModel.SUCCESS(service.update(entity));
    }

    @ResponseBody
    @DeleteMapping("{id}")
    public ResultModel delete(@NotNull @PathVariable("id") String id) {
        return ResultModel.SUCCESS(service.deleteByIds(id.split(",")));
    }

    @ResponseBody
    @GetMapping("list")
    public ResultModel list(@NotNull @RequestBody O entity, HttpServletRequest request, HttpServletResponse response) {
        return ResultModel.SUCCESS(service.findByPage(entity, "", null));
    }
}
